//
//  DFStatusRetweetedView.swift
//  FlyBlog
//
//  Created by FEI on 16/4/15.
//  Copyright © 2016年 FEI. All rights reserved.
//

import UIKit

class DFStatusRetweetedView: UIView {

    var retweetStatus: DFStatus? {
        didSet {
            let retweetName = retweetStatus?.user?.screen_name ?? "被转发微博的用户名称"
            let retweetText = retweetStatus?.text ?? "被转发的微博内容为空"
            let retweetContent = "@\(retweetName): \(retweetText)"

            retweetLabel.text = retweetContent
            retweetPictureView.pictureURLs = retweetStatus?.storePictureURLs

            retweetPictureView.snp_updateConstraints { (make) -> Void in
                let margin = retweetStatus?.storePictureURLs == nil ? 0 : DFStatusCellMargin
                make.top.equalTo(retweetLabel.snp_bottom).offset(margin)
            }
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("")
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }

    private func prepareUI() {
        addSubview(backButton)
        addSubview(retweetLabel)
        addSubview(retweetPictureView)

        backButton.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self)
        }

        retweetLabel.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(DFStatusCellMargin)
                make.trailing.equalTo(self).offset(-DFStatusCellMargin)
        }

        retweetPictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(DFStatusCellMargin)
            make.top.equalTo(retweetLabel.snp_bottom).offset(DFStatusCellMargin)
            make.size.equalTo(CGSizeZero)
        }

        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(retweetPictureView).offset(DFStatusCellMargin)
        }

    }

    //MARK: 懒加载
    private lazy var backButton: UIButton = { () -> UIButton in
        let button = UIButton()

        button.setBackgroundImage(UIImage(named: "retweet_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "retweet_background_highlighted"), forState: UIControlState.Highlighted)
        return button
    }()

    private lazy var retweetLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(DFStatusContentFontSize)
        label.textColor = UIColor.darkGrayColor()
        label.numberOfLines = 0
        return label
    }()

    private lazy var retweetPictureView: DFStatusPictureView = DFStatusPictureView()

}
